package com.ruoyi.carton.controller;


import com.ruoyi.carton.domain.bo.OrderRequestWithCardboardBO;
import com.ruoyi.carton.domain.vo.OrderRequestQuery;
import com.ruoyi.carton.domain.vo.OrderRequestVO;
import com.ruoyi.carton.service.OrderRequestService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/request")
public class OrderRequestController extends BaseController {

    @Autowired
    private OrderRequestService orderRequestService;

    /**
     * 保存或更新纸板信息，并绑定到指定的请购单
     *
     * @param requestId 请购单ID
     * @param vo        纸板信息及绑定信息
     * @return AjaxResult 成功或失败
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-carton:request:edit')")
    @Log(title = "请购单", businessType = BusinessType.UPDATE)
    @PostMapping("/saveOrUpdateCardboard/{id}")
    public AjaxResult saveOrUpdateCardboard(@PathVariable("id") Integer requestId,
                                            @RequestBody OrderRequestVO vo) {
        orderRequestService.addOrUpdateCardboardAndBindToRequest(requestId, vo);
        return success("保存/更新成功");
    }

    /**
     * 查询请购单详情，包括绑定的纸板信息
     *
     * @param id 请购单ID
     * @return AjaxResult 数据详情
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-carton:request:query')")
    @Log(title = "请购单", businessType = BusinessType.OTHER)
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable("id") Integer id) {
        OrderRequestWithCardboardBO bo = orderRequestService.getWithCardboardById(id);
        return success(bo);
    }

    /**
     * 获取请购单列表（分页），包含纸箱和纸板信息
     *
     * @param query 查询条件
     * @return TableDataInfo 分页列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-carton:request:list')")
    @Log(title = "请购单", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(OrderRequestQuery query) {
        startPage();
        List<OrderRequestWithCardboardBO> list = orderRequestService.listWithBoxAndCardboard(query);
        return getDataTable(list);
    }

    /**
     * 删除指定的请购单及其绑定的纸板信息，
     * 并将订单状态回退为草稿（DRAFT）
     *
     * @param id 请购单ID
     * @return AjaxResult 操作结果
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-carton:request:remove')")
    @Log(title = "请购单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Integer id) {
        try {
            orderRequestService.removeOrderRequestWithCardboard(id);
            return AjaxResult.success("删除成功");
        } catch (ServiceException e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
